package com.hjm.app.fragment.user;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.hjm.app.R;
import com.hjm.java.entity.UserInfo;
import com.hjm.java.utils.HjmObjectCallBack;

/**
 * 用户信息详情
 */
public class FragmentUserInfo extends Fragment {
    private static final String TAG = "FragmentUserInfo";
    private LinearLayout linearLayoutUserInfoEdit,linearLayoutUserInfoShow;
    private ImageView imageViewUserInfoProfilePhoto;
    private EditText editTextUserInfoNickName,editTextUserInfoPassword;
    private TextView textViewUserInfoNickName,textViewUserInfoLoginName;
    private Button buttonUserInfoEdit,buttonUserInfoCommit,buttonUserInfoLogout, buttonUserInfoCommitCancel;
    private UserInfoListener userInfoListener;
    private UserInfo userInfo;
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public FragmentUserInfo(UserInfoListener userInfoListener)  {
        this.userInfoListener = userInfoListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取用户信息
        userInfoListener.onGetUserInfo((isSuccess, userInfo, msg) -> {
            if (isSuccess){
                this.userInfo = userInfo;
                setUserInfo(userInfo);
            } else {
                Toast.makeText(getContext(),"获取用户信息失败 "+msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    void setEditVisible(boolean isEdit){
        if (!isEdit){
            linearLayoutUserInfoEdit.setVisibility(View.GONE);
            linearLayoutUserInfoShow.setVisibility(View.VISIBLE);
        } else {
            linearLayoutUserInfoEdit.setVisibility(View.VISIBLE);
            linearLayoutUserInfoShow.setVisibility(View.GONE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_user_info, container, false);
        linearLayoutUserInfoEdit = view.findViewById(R.id.layout_user_info_edit);
        linearLayoutUserInfoShow = view.findViewById(R.id.layout_user_info_show);

        imageViewUserInfoProfilePhoto = view.findViewById(R.id.image_view_user_info_profile_photo);

        editTextUserInfoNickName = view.findViewById(R.id.edit_text_user_info_nick_name);
        editTextUserInfoPassword = view.findViewById(R.id.edit_text_user_info_password);

        textViewUserInfoNickName = view.findViewById(R.id.text_view_user_info_nick_name);
        textViewUserInfoLoginName = view.findViewById(R.id.text_view_user_info_login_name);

        buttonUserInfoEdit = view.findViewById(R.id.button_user_info_edit);
        buttonUserInfoCommit = view.findViewById(R.id.button_user_info_commit);
        buttonUserInfoLogout = view.findViewById(R.id.button_user_info_logout);
        buttonUserInfoCommitCancel = view.findViewById(R.id.button_user_info_commit_cancel);

        buttonUserInfoEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEditVisible(true);
            }
        });

        buttonUserInfoCommitCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 提交修改
                setEditVisible(false);
            }
        });

        buttonUserInfoCommit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 提交修改
                UserInfo userInfo = new UserInfo();
                String password = editTextUserInfoPassword.getText().toString();
                if (password.length()<6){
                    password = null;
                }
                userInfo.setNickName(editTextUserInfoNickName.getText().toString());
                userInfo.setPasswordMd5(password);
                // TODO:可能需要其他内容
                userInfoListener.onUserInfoCommit(userInfo);
            }
        });

        buttonUserInfoLogout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 退出登录
                userInfoListener.onUserInfoLogout();
            }
        });
        return view;
    }
    public void setUserInfo(UserInfo userInfo){
        if (userInfo != null){
            this.userInfo = userInfo;
            editTextUserInfoNickName.setText(userInfo.getNickName());
            textViewUserInfoNickName.setText(userInfo.getNickName());
            textViewUserInfoLoginName.setText(userInfo.getLoginName());
            editTextUserInfoPassword.setText("");
            if (userInfo.getProfilePhotoBitmap() != null)
                imageViewUserInfoProfilePhoto.setImageBitmap(userInfo.getProfilePhotoBitmap());
            setEditVisible(false);
        }
    }

    public interface UserInfoListener {
        void onBackClick();
        void onUserInfoCommit(UserInfo userInfo);
        void onUserInfoLogout();
        void onGetUserInfo(HjmObjectCallBack<UserInfo> callBack);
    }
}